package com.xbongbong.paas.form;

import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.paas.help.FormDataHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.WorkOrderExplainEntity;
import com.xbongbong.saas.domain.entity.ext.WorkOrderFormEntityExt;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.PaasFormDataModel;
import com.xbongbong.saas.model.WorkOrderExplainModel;
import com.xbongbong.saas.model.WorkOrderFormModel;
import com.xbongbong.saas.model.WorkReportModel;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 表单抽象类
 * @author zheng.li
 **/
public abstract class AbstractFormService implements FormInterface {


    @Resource
    private WorkOrderFormModel workOrderFormModel;

    @Resource
    private WorkOrderExplainModel workOrderExplainModel;

    @Resource
    private PaasFormModel paasFormModel;

    @Resource
    private PaasFormExplainModel paasFormExplainModel;

    @Resource
    private PaasFormDataModel paasFormDataModel;

    @Resource
    private WorkReportModel workReportModel;

    @Resource
    private FormDataHelp formDataHelp;

    @Override
    public PaasFormEntityExt getForm(String corpid, Long formId, Integer businessType) throws XbbException {
        PaasFormEntityExt form = new PaasFormEntityExt();
        if (Objects.equals(businessType, XbbRefTypeEnum.WORK_ORDER.getCode())) {
            WorkOrderFormEntityExt workOrderForm = workOrderFormModel.getByKey(formId,corpid);
            if (workOrderForm == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_NOT_EXIST);
            }
            BeanUtil.copyProperties(workOrderForm, form);
            form.setSaasMark(SaasMarkEnum.SAAS.getCode());
            form.setBusinessType(XbbRefTypeEnum.WORK_ORDER.getCode());
        } else {
            form = paasFormModel.getByKey(formId, corpid);
        }
        if (form == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_NOT_EXIST);
        }
        return form;
    }

    @Override
    public PaasFormExplainEntity getFormExplain(String corpid, Long formId, Integer businessType) throws XbbException {
        PaasFormExplainEntity paasFormExplainEntity = new PaasFormExplainEntity();
        if (Objects.equals(businessType, XbbRefTypeEnum.WORK_ORDER.getCode())) {
            //工单
            WorkOrderExplainEntity workOrderExplainEntity = workOrderExplainModel.getByFormId(formId, corpid);
            if (Objects.isNull(workOrderExplainEntity)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
            }
            BeanUtil.copyProperties(workOrderExplainEntity, paasFormExplainEntity);
        } else {
            //其他
            paasFormExplainEntity = paasFormExplainModel.getByFormId(formId, corpid);
            if (Objects.isNull(paasFormExplainEntity)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
            }
        }
        return paasFormExplainEntity;
    }

    @Override
    public PaasFormDataEntityExt getFormData(Long dataId, String corpid, Integer businessType) throws XbbException {
        PaasFormDataEntityExt paasFormDataEntityExt;
        if ( Objects.equals(businessType,0) ) {
            paasFormDataEntityExt = paasFormDataModel.getByKey(dataId,corpid);
        } else {
            paasFormDataEntityExt = formDataHelp.getFormData(dataId, corpid, businessType);
        }
        if (paasFormDataEntityExt == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
        }
        return paasFormDataEntityExt;
    }

    @Override
    public Map<String, FieldAttrEntity> getExplainMap(List<FieldAttrEntity> explainList, Integer businessType, String corpid, Long formId) {
        Map<String, FieldAttrEntity> expalinMap = new HashMap<>(explainList.size());
        for (FieldAttrEntity fieldAttrEntity : explainList) {

            if (Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(),fieldAttrEntity.getFieldType())){
                SubFormPoJo subForm = fieldAttrEntity.getSubForm();
                if (subForm != null && subForm.getItems() != null) {
                    List<? extends FieldAttrEntity> subExplainList = subForm.getItems();
                    Iterator<? extends FieldAttrEntity> iterator = subExplainList.iterator();
                    while (iterator.hasNext()){
                        FieldAttrEntity subFieldAttrEntity = iterator.next();
                        if (Objects.equals(subFieldAttrEntity.getAttr(), SelectProductEnum.PRODUCT.getAttr())){
                            subFieldAttrEntity.setFieldType(FieldTypeEnum.TEXT.getType());
                            break;
                        }
                    }
                }
            }
            expalinMap.put(fieldAttrEntity.getAttr(),fieldAttrEntity);
        }
        return expalinMap;
    }


}
